package edu.sctu.zhuyingzhi.month201505.day20150519.threadTest;

/**
 * Created by user13 on 2015/5/19.
 */
public class Cannons {
    private boolean shells = false;
    private int i,j = 0;

    synchronized void shot(){                       //发射
        while(!shells){
            try{
                wait();
            }catch (Exception e){
                System.out.println("程序中断");
            }
        }
        shells = false;
        i++;
        if (i>5)
            System.exit(1);
        System.out.println("大炮第"+i+"次发射");
        notify();
    }

    synchronized void load(){                       //填装
        while(shells){
            try{
                wait();
            }catch (Exception e){
                System.out.println("程序中断");
            }
        }
        shells = true;
        j++;
        if (j>5){
            System.exit(1);
        }
        System.out.println("炮弹填充完毕 ");
        notify();
    }

}
